<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Tutorial: Changes API</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Tutorial: Changes API</h1>

    <section>

<header>
    

    <h2>Changes API</h2>
</header>

<article>
    <h1>Overview</h1><p>LokiJS 1.1 introduces a &quot;Changes API&quot; that enables the user to keep track of the changes happened to each collection since a particular point in time, which is usually the start of a work session but it could be a user defined one.
This is particularly useful for remote synchronization.</p>
<h2>Description of the Changes API</h2><p>The Changes API is a collection-level feature, hence you can establish which collections may simply contain volatile data and which ones need to keep a record of what has changed.</p>
<p>The Changes API is an optional feature and can be activated/deactivated by either passing the option <code>{ disableChangesApi: isDisabled }</code> in the config parameter of a collection constructor, or by calling <code>collection.setChangesApi(isEnabled)</code>.
Note that LokiJS will always set the fastest performing setting as default on a collection or database, hence the Changes API is <strong>disabled</strong> by default.</p>
<p>There are three events which will trigger a Changes API operation: inserts, updates and deletes.
When either of these events occur, on a collection with Changes API activated, the collection will store a snapshot of the relevant object, associated with the operation and the name of the collection.</p>
<p>From the database object it is then possible to invoke the <code>serializeChanges</code> method which will generate a string representation of the changes occurred to be used for synchronization purposes.</p>
<h2>Usage</h2><p>To enable the Changes API make sure to either instantiate a collection using <code>db.addCollection('users', { disableChangesApi: false })</code>, or call <code>users.setChangesApi(true)</code> (given an example <code>users</code> collection).</p>
<p>To generate a string representation of the changes, call <code>db.serializeChanges()</code>. This will generate a representation of all the changes for those collections that have the Changes API enabled. If you are only interested in generating changes for a subset of collections, you can pass an array of names of the collections, i.e. <code>db.serializeChanges(['users']);</code>.</p>
<p>To clear all the changes, call <code>db.clearChanges()</code>. Alternatively you can call <code>flushChanges()</code> on the single collection, normally you would call <code>db.clearChanges()</code> on a callback from a successful synchronization operation.</p>
<p>Each change is an object with three properties: <code>name</code> is the collection name, <code>obj</code> is the string representation of the object and <code>operation</code> is a character representing the operation (&quot;I&quot; for insert, &quot;U&quot; for update, &quot;R&quot; for remove). So for example, inserting user <code>{ name: 'joe' }</code> in the users collection would generate a change <code>{ name: 'users', obj: { name: 'joe' }, operation: 'I' }</code>. Changes are kept in order of how the happened so a 3rd party application will be able to operate insert updates and deletes in the correct order.</p>
</article>

</section>

</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Collection.html">Collection</a></li><li><a href="DynamicView.html">DynamicView</a></li><li><a href="Loki.html">Loki</a></li><li><a href="LokiEventEmitter.html">LokiEventEmitter</a></li><li><a href="LokiFsAdapter.html">LokiFsAdapter</a></li><li><a href="LokiFsStructuredAdapter.html">LokiFsStructuredAdapter</a></li><li><a href="LokiIndexedAdapter.html">LokiIndexedAdapter</a></li><li><a href="LokiLocalStorageAdapter.html">LokiLocalStorageAdapter</a></li><li><a href="LokiMemoryAdapter.html">LokiMemoryAdapter</a></li><li><a href="LokiPartitioningAdapter.html">LokiPartitioningAdapter</a></li><li><a href="Resultset.html">Resultset</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-Autoupdating Collections.html">Autoupdating Collections</a></li><li><a href="tutorial-Changes API.html">Changes API</a></li><li><a href="tutorial-Collection Transforms.html">Collection Transforms</a></li><li><a href="tutorial-Indexing and Query performance.html">Indexing and Query performance</a></li><li><a href="tutorial-Loki Angular.html">Loki Angular</a></li><li><a href="tutorial-Persistence Adapters.html">Persistence Adapters</a></li><li><a href="tutorial-Query Examples.html">Query Examples</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu May 10 2018 03:42:33 GMT-0400 (Eastern Daylight Time)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>